
local string_utils = require("mvc.utils.string_utils");


local form_validate = {

	handlers = {
	
	},
	
	MSG = {
		must = '不能为空',
		maxlength = '长度不能大于#max',
		minlength = '长度不能小于#min',
		equalTo = '两次输入的值不一致',
		email = 'Email地址格式错误',
		url = 'url地址格式错误',
		ip = 'ip地址格式错误',
		int = '只能为整数',
		float = '只能为浮点数',
		mobile = '手机号码格式错误',
		idCard = '身份证号码格式错误',
		max = '值不能大于#max',
		min = '值不能小于#min'
	}
};

--[[

	表单验证
	
	local form_validation = require("config.form_validation");
	local form_validate = require("mvc.form.form_validate");
	local form_handler = require("mvc.form.form_handler"):new();
	
	local form = form_handler.get_form();
	local validate_result = form_validate.do_validate(form_validation.form_name_1, form);
	
	if !validate_result.success then
		--显示表单项错误信息
		validate_result.messages
	end
	
	返回结果table
	{
		success = false,
		messages = {
			userName = {"不能为空", "长度不能大于xx"},
			email = {"不是标准的邮箱地址"}
		}
	}
]]
function form_validate.do_validate(config, form)

	local vr = {};

	local success = true;
	for i, t in pairs(config) do
		local field_name = i;
		local field_conf = t;
		local display = field_conf.display;
		local validator = field_conf.validator;
		local fiv = form[field_name];
		
		local messages = {};
		local message_arr = {};
			
		for j = 1, #validator, 1 do
			local vi = validator[j];
			
			--[[
				处理必填项
			]]
			if (vi["must"] ~= nil and vi["must"] == true) then
				if (fiv == nil or #fiv < 0) then
					local m = string.gsub(vi.message, "{1}", display);
					table.insert(message_arr, 1, m);
					success = false;
				end
			end
			
			
			--[[
				处理最大长度
			]]
			if (vi["maxlength"] ~= nil) then
			
				--ngx.say("validator.message", vi.message);
				--ngx.say("fiv", fiv, "=====", #fiv);
				
				if (#fiv > vi["maxlength"]) then
					local m = string.gsub(vi.message, "{1}", display);
					m = string.gsub(m, "{maxlength}", vi["maxlength"]);
					table.insert(message_arr, 1, m);
					success = false;
				end

			end
			
			--[[
				处理最小长度
			]]
			if (vi["minlength"] ~= nil) then
				if (#fiv < vi["minlength"]) then
					local m = string.gsub(vi.message, "{1}", display);
					m = string.gsub(m, "{minlength}", vi["minlength"]);
					table.insert(message_arr, 1, m);
					success = false;
				end
			end
			
			--[[
				处理与另一个字段相等
			]]
			if (vi["equalTo"] ~= nil) then
				if (#fiv ~= form[vi["equalTo"]]) then
					local m = string.gsub(vi.message, "{1}", display);
					table.insert(message_arr, 1, m);
					success = false;
				end
			end
			
			--[[
				处理扩展验证组件
			]]
			if (vi["handler"] ~= nil) then
				local hf = form_validate.handlers[vi["handler"]];
				if (hf == nil) then
					table.insert(message_arr, 1, "没找到对应的验证处理器");
					success = false;
				end
				local r = hf(fiv);
				if (r ~= true) then
					local m = string.gsub(vi.message, "{1}", display);
					table.insert(message_arr, 1, m);
					success = false;
				end
			end
			
			messages[field_name] = message_arr;
		end
		
		vr["messages"] = messages;
	end
	
	vr["success"] = success;
	return vr;
end


---------------------------------------- 扩展handlers ----------------------------------------------
--[[
	Email验证器
]]
function form_validate.handlers.email(value)
	return string.match(value,"[%w]+@[%w.]+") == value;
end

--[[
	URL验证器
]]
function form_validate.handlers.url(value)
	--return string.match(value,"http[s]?://[\.a-zA-Z0-9\-_]+") == value;
end

--[[
	IP验证器
]]
function form_validate.handlers.ip(value)
	
end

--[[
	手机号验证器
]]
function form_validate.handlers.mobile(value)
	
end


--[[
	整型数字验证器
]]
function form_validate.handlers.number(value)
	return form_validate.handlers.is_number(value);
end

--[[
	身份证号验证器
]]
function form_validate.handlers.id_card(value)
	
end

function form_validate.handlers.is_number(value)
	local n = tonumber(value);
	if n then
		return true;
	else
		return false;
	end
end

return form_validate;

